package com.coolw.spring.component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * bean 生命周期中各方法的执行顺序
 *
 * @author coolw
 * @date 2024/1/10 11:27
 */
@Component
public class BeanLifeMethod implements InitializingBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware, DisposableBean {

    public BeanLifeMethod() {
        System.out.println("BeanLifeMethod#参构造方法执行......");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("BeanLifeMethod#afterPropertiesSet方法执行......");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("BeanLifeMethod#postConstruct方法执行......");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanLifeMethod#setBeanFactory方法执行......");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("BeanLifeMethod#setBeanName方法执行......");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("BeanLifeMethod#destroy方法执行......");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("BeanLifeMethod#setApplicationContext方法执行......");
    }

    @Bean(initMethod = "init")
    public BeanLifeMethod test() {
        return new BeanLifeMethod();
    }

    public void init() {
        System.out.println("BeanLifeMethod#init方法执行......");
    }
}

